package com.raymond.queue.client.codec.encodeing;

import com.raymond.queue.common.codec.annotation.MsgCodec;
import com.raymond.queue.common.codec.encodeing.BaseEncoder;
import com.raymond.queue.common.enums.CommandIdType;
import com.raymond.queue.common.msg.BaseMessage;
import com.raymond.queue.common.msg.request.ProductionMsgRequest;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

/**
 * 消费者请求编码器
 *
 * @author :  raymond
 * @version :  V1.0
 * @date :  2021-07-14 11:39
 */
@MsgCodec(commandId = CommandIdType.PRODUCTION_REQ)
public class ProductionRequestEncoder extends BaseEncoder {


    @Override
    public byte[] encode0(BaseMessage msg) {
        ProductionMsgRequest productionRequest = (ProductionMsgRequest) msg;
        byte[] bytes = productionRequest.getTopic().getBytes();
        ByteBuf buffer = Unpooled.buffer();
        buffer.writeInt(bytes.length);
        buffer.writeBytes(bytes);
        buffer.writeBytes(productionRequest.getData());
        byte[] body = new byte[bytes.length + productionRequest.getData().length + 4];
        buffer.readBytes(body);
        return body;
    }
}
